<?php

class Sistema_Form_Interesse extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_'.$controller);
        
        $id = $front->getRequest()->getParam('id', 0);

        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $query = 'SELECT SQL_CACHE * FROM INTERESSE WHERE INT_ID = ' . $id;
        $data = $db->fetchRow($query);
        
        Zend_Loader::loadClass('dominio','library');
        
        
        $el = new Zend_Form_Element_Hidden('INT_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('INT_NOM');
        $el->setLabel('Nome')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))->addValidator('NotEmpty');
        $el->setAttrib('required','');
        $el->setAttrib('readonly','');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Text('INT_TEL');
        $el->setLabel('Telefone')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))->addValidator('NotEmpty');
        $el->setAttrib('required','');
        $el->setAttrib('readonly','');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('INT_EML');
        $el->setLabel('E-mail')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))->addValidator('NotEmpty');
        $el->setAttrib('required','');
        $el->setAttrib('readonly','');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Textarea('INT_MSG');
        $el->setLabel('Mensagem');
        $el->setAttrib('readonly','');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Text('INT_DAT');
        $el->setLabel('Data')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))->addValidator('NotEmpty');
        $el->setAttrib('required','');
        $el->setAttrib('readonly','');
        $this->addElement($el);
        
        
        if(strlen($data['COR_ID']) > 1){ 
            $html = '<ul>';
            
            $query = 'SELECT SQL_CACHE COR_ID,COR_NOM FROM CORES WHERE COR_ID in('.$data['COR_ID'].') ORDER BY COR_NOM ASC';
            $result = $db->fetchAll($query);
            foreach ($result as $row){ 
                
                $imgs = glob('public/upload/cores/'.$row['COR_ID'].'.*');
                $img = '';
                if($imgs[0]) $img = '<img src="'.$front->getBaseUrl().'/'.$imgs[0].'" style="vertical-align: middle;" />';
                
                $html .= '<li>'.$img.' '.$row['COR_NOM'].'</li>';
                
            }
            
            $html .= '</ul>';
        
            
            $html = '<div id="cmb_cores">'.$html.'</div>';
        
            $el = new Zend_Form_Element('COR_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
            $el->setLabel('Cores');
            $this->addElement($el); 
        }
        
        if(strlen($data['ACA_ID']) > 1){ 
            $html = '<ul>';
            
            $query = 'SELECT SQL_CACHE ACA_ID,ACA_NOM FROM ACABAMENTOS WHERE ACA_ID in('.$data['ACA_ID'].') ORDER BY ACA_NOM ASC';
            $result = $db->fetchAll($query);
            foreach ($result as $row){ 
                
                $imgs = glob('public/upload/acabamentos/'.$row['ACA_ID'].'.*');
                $img = '';
                if($imgs[0]) $img = '<img src="'.$front->getBaseUrl().'/'.$imgs[0].'" style="vertical-align: middle;" />';
                
                $html .= '<li>'.$img.' '.$row['ACA_NOM'].'</li>';
                
            }
            
            $html .= '</ul>';
        
            
            $html = '<div id="cmb_cores">'.$html.'</div>';
        
            $el = new Zend_Form_Element('ACA_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
            $el->setLabel('Acabamentos');
            $this->addElement($el); 
        }
        
        
        
        $arr = dominio::statusInteresse();
        $el = new Zend_Form_Element_Select('INT_STACNT',array('onchange' => ''));
        $el->setLabel('Status Contato');
        $el->addMultiOptions($arr);
        $this->addElement($el);
        
        /*
        $el = new Zend_Form_Element_Checkbox('INT_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        */
        
        $btn = new Zend_Form_Element_Submit('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setLabel('Salvar');
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
    }


}

